/*
 * @Author: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @Date: 2025-08-30 17:22:35
 * @LastEditors: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @LastEditTime: 2025-08-31 17:49:11
 * @FilePath: \keep_motion_api\controllers\permission\role.controller.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const { Role } = require('@/models/permission');

// 查询角色
const getRole = async (req, res) => {
  try {
    const { name, code, pageNum, pageSize } = req.query;
    const offset = (pageNum - 1) * pageSize;
    const limit = pageSize;
    const {rows, total} = await Role.getRole(name, code, offset, limit);
    res.status(200).send({rows, total});
  } catch (error) {
    res.status(500).json({ message: '查询角色失败!', error });
  }
}

// 创建角色
const createRole = async (req, res) => {
  try {
    const { name, code, description } = req.body;
    await Role.createRole(name, code, description);
    res.status(200).send({message: '创建角色成功!'});
  } catch (error) {
    res.status(500).json({ message: '创建角色失败!', error });
  }
}

// 删除角色
const deleteRole = async (req, res) => {
  try {
    const { id } = req.params;
    await Role.deleteRole(id);
    res.status(200).send({message: '删除角色成功!'});
  } catch (error) {
    res.status(500).json({ message: '删除角色失败!', error });
  }
}

// 更新角色权限
const updateRolePermission = async (req, res) => {
  try {
    const { id } = req.params;
    const { permissionIds } = req.body;
    await Role.updateRolePermission(id, permissionIds);
    res.status(200).send({message: '更新角色权限成功!'});
  } catch (error) {
    res.status(500).json({ message: '更新角色权限失败!', error });
  }
}



module.exports = {
    getRole,
    createRole,
    updateRolePermission,
    deleteRole
}
